﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;



namespace HR60_PanelSoft.Service
{
    //GC: [g1]场景的删除逻辑[SceneService]
    //GC: [g2]NewReq：增加场景映射层[SceneService]
	public class SceneService : CommonService
	{
		public static Scene findByUuid(string uuid)
		{
			foreach (Scene scene in proj.scenes)
			{
				if (scene.uuid == uuid)
					return scene;
			}
			return null;
		}

		public static List<Scene> findListByUuid(List<string> uuids)
		{
			List<Scene> list = new List<Scene>();
			foreach (string uuid in uuids)
			{
				list.Add(findByUuid(uuid));
			}
			return list;
		}
		
		// 添加
		public static void append(Scene scene)
		{
			proj.scenes.Add (scene);
		}

		// 删除
		public static void remove(Scene scene)
		{

            /* [g1.s1] 卸载场景按键映射 */
            foreach (HR60_PanelSoft.Model.Net n in proj.nets)
            {
                foreach (Panel p in n.panels)
                {
                    if (p.scenePages.Count > 0)
                        foreach (PanelPage pp in p.scenePages)
                        {
                            if (pp.buttons.Count < 1)
                                continue;
                            foreach(PanelButton pb in pp.buttons)
                            {
                                if (pb.mappingScene == scene.uuid)
                                {
                                    pb.mappingScene = null;
                                }
                            }
                        }
                    /* [g1.s2] 卸载外部信号映射 */
                    if (p.signal.scene == scene.uuid)
                        p.signal.scene = null;
                    /* end of [g1.s2] */
                }
            }
            /* end of [g1.s1] */





			proj.scenes.Remove (scene);
		}

        /* [g2.s1] */
        public static DataTable createTableByScn(Device dev, Scene scn)
        {
            if (dev.type.name.Contains("分机"))
                return null;
            DataTable rst = new DataTable();
            //DataRow row = new DataRow();
            //SceneEvent sEvent = new SceneEvent();



            //foreach (SceneEvent e in sne.events)
            //{
            //    if (e.device == dev.uuid)
            //    {
            //        sEvent = e;
            //        break;
            //    }
            //}

            /* create constant columns */
            DataColumn col1 = new DataColumn("dev");
            col1.Unique = false;
            col1.DataType = typeof(string);
            rst.Columns.Add(col1);

            /* create automatic columns */
            int colIdx = 0;
            foreach (CtrlAttr a in dev.childDevices[0].type.attrs)
            {
                DataColumn tempCol = new DataColumn();
                tempCol.Unique = false;
                tempCol.DataType = typeof(string);
                tempCol.ColumnName = "prop" + colIdx.ToString();
                rst.Columns.Add(tempCol);
                DataColumn tempBind = new DataColumn();
                tempCol.Unique = false;
                tempCol.DataType = typeof(string);
                tempBind.ColumnName = "bind" + colIdx.ToString();
                rst.Columns.Add(tempBind);
                colIdx++;
            }

            /* fill table */
            if (dev.type.exType != ExDeviceType.NULL)
            {
                foreach (Device sub in dev.childDevices)
                {
                    bool isInEvt = false;
                    SceneEvent tempEvt = null;
                    DataRow tempRow = rst.NewRow();
                    object[] rowDatus = new object[rst.Columns.Count];
                    int arrIdx = 0;

                    rowDatus[arrIdx++] = sub.nickName; //first cell
                    foreach (SceneEvent se in scn.events)
                    {
                        if (se.device == sub.uuid)
                        {
                            isInEvt = true;
                            tempEvt = se;
                            break;
                        }
                    }
                    if (isInEvt)
                    {

                        foreach (CtrlAttr a in sub.type.attrs)
                        {
                            int findIdx = -1;
                            foreach (string s in tempEvt.attrs)
                            {
                                findIdx++;
                                if (s == a.name)
                                {
                                    break;
                                }
                                if (findIdx == tempEvt.attrs.Count - 1)
                                {
                                    findIdx = -1;
                                }
                            }
                            if (findIdx != -1)
                            {//exsits in event
                                rowDatus[arrIdx++] = a.GetKeybyValue(tempEvt.attrValueKeys[findIdx]);
                                rowDatus[arrIdx++] = "true";
                            }
                            else
                            {
                                rowDatus[arrIdx++] = a.values.ElementAt(0).Key;//默认首选项
                                rowDatus[arrIdx++] = "false";
                            }

                        }
                        isInEvt = false;
                    }
                    else
                    {
                        foreach (CtrlAttr a in sub.type.attrs)
                        {
                            rowDatus[arrIdx++] = a.values.ElementAt(0).Key;//默认首选项
                            rowDatus[arrIdx++] = "false";
                        }
                    }

                    tempRow.ItemArray = rowDatus;
                    rst.Rows.Add(tempRow);

                }
            }
            else
            {

            }


            return rst;
        }
        /* end of [g2.s1] */
	}
}
